<?php
/**
 * 安全控制器
 * 调用接口通过这个控制器判断用户是否可用
 */
namespace Api\Controller;
use Think\Controller;
class AController extends Controller {
    public function _initialize(){
        //$key = I('key');
        //$my_key = md5(strtotime(date('Y-m-d H',time())).'0123456789');
        //if($key != $my_key){
        //    echo 'key错误~';
        //    exit;
        //}
    }
    //获取手机验证
    function phone_verify($phone){
        $min = pow(10 , (6 - 1));
        $max = pow(10, 6) - 1;
        $verify = rand($min, $max);
        $verify_data = array(
            'login_name'=>$phone,
            'verify'    =>$verify,
            'time'      =>time()
        );
        M('verify_code')->add($verify_data);
        $msg = "您的验证码是：".$verify."。请不要把验证码泄露给其他人。如非本人操作，可不用理会！ ";
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        //多个手机号码请用英文,号隔开
        $post_data = "account=C30495780&password=853766c56e82074c8a9eafa39c730d32&mobile=".$phone."&content=".rawurlencode($msg);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $target);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        $return_str = curl_exec($curl);
        curl_close($curl);
        $return_str = xmlToArray($return_str);
        $return_str['verify']=$verify;
        return $return_str;
    }
    //获取邮箱验证码接口
    function email_verify($to){
        $min = pow(10 , (6 - 1));
        $max = pow(10, 6) - 1;
        $verify = rand($min, $max);
        $verify_data = array(
            'login_name'=>$to,
            'verify'    =>$verify,
            'time'      =>time()
        );
        M('verify_code')->add($verify_data);
        $content = '亲爱的用户：您好！感谢您使用凤凰云服务，您正在进行邮箱验证，本次请求的验证码为：'.$verify.'(为了保障您帐号的安全性，请在1小时内完成验证。)';
        $title ="邮箱验证码";
        // 配置邮件发送服务器
        Vendor('PHPMailer.PHPMailerAutoload');
        $mail = new \PHPMailer(); //实例化
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host='smtp.163.com'; //smtp服务器的名称
        $mail->SMTPAuth = TRUE; //启用smtp认证
        $mail->Username = 'by_fenghuang417@163.com'; //你的邮箱名
        $mail->Password = 'bayin123456' ; //邮箱密码
        $mail->From = 'by_fenghuang417@163.com'; //发件人地址（也就是你的邮箱地址）
        $mail->FromName = '凤凰八音云平台'; //发件人姓名
        $mail->AddAddress($to,"尊敬的客户");
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML(TRUE); // 是否HTML格式邮件
        $mail->CharSet='utf-8'; //设置邮件编码
        $mail->Subject =$title; //邮件主题
        $mail->Body = $content; //邮件内容
        $mail->AltBody = "这是一个纯文本的,非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
        $return['code']=$mail->Send();
        $return['verify']=$verify;
        return $return;
    }
    //验证两次输入的密码是否相同
    function check_password($password,$re_password){
        if($password != $re_password){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'两次输入密码不一致(Input passwords are not consistent)',
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //验证手机号是否符合要求
    function check_phone($phonenumber){
        $res = preg_match("#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#",$phonenumber);
        if(!$res){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'手机号码格式错误(Cell phone number format error)'
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //验证手机验证码或邮箱验证码是否正确
    function check_verify($verify_code,$str,$type){
        if(!$verify_code){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'请输入验证码(Please input validation)'
                )
            );
            $this->ajaxReturn($arr);
        }
        //查询最后一条验证码
        $w = array(
            'login_name'=>$str,
        );
        $res = M('verify_code')->order('time desc')->find();
        if(time()-$res['time']>=3600){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'验证码过期'
                )
            );
            $this->ajaxReturn($arr);
        }
        $verify=$res['verify'];
        if($type=='phone'){
            $msg ='手机验证码错误(Cell phone verification code error)';
        }
        if($type=='email'){
            $msg = '邮箱验证码错误(Email verification code error)';
        }
        if($verify != $verify_code){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>$msg
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //验证邮箱格式是否正确
    function check_email($email){
        $res = preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$email);
        if(!$res){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'邮箱格式错误(Email format error)'
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //查找手机号是否已经注册
    function check_phone_register($w){
        $res = M('user')->where($w)->find();
        if($res){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'手机号已经注册(The phone number has been registered)'
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //查询邮箱是否已经注册
    function check_email_register($w){
        $res = M('user')->where($w)->find();
        if($res){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'邮箱已经注册(The email has been registered)'
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //查询用户名是否已经注册
    function check_name_register($w){
        $res = M('user')->where($w)->find();
        if($res){
            $info['name']=$res['name'];
            $info['phone']=$res['phone'];
            $info['email']=$res['email'];

            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'用户名/手机/邮箱)已经注册(The username/phone/email has been registered)',
                    'info'=>$info
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //判断uid是否是整数
    function check_uid($uid){
        if(!is_numeric($uid)){
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'请输入正确的uid(Please enter the correct uid)',
                    'uid'=>$uid
                )
            );
            $this->ajaxReturn($arr);
        }
    }
    //查询数据库中是否有传入id的用户
    function check_user($uid){
        $user = M('user')->where('id='.$uid)->find();
        if($user){
            return $user;
        }else{
            $arr = array(
                'code'=>0,
                'result'=>array(
                    'msg'=>'没有找到用户信息(No user information found)'
                )
            );
            $this->ajaxReturn($arr);
        }
    }
}